package com.ewanghuiju.app.ui.shopmail.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseFragment;
import com.ewanghuiju.app.base.contract.shopmail.ShopmailContract;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.bean.local.VipEquityBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.presenter.shopmail.ShopmailPresenter;
import com.ewanghuiju.app.test.bean.PintuanHomeBannerBean;
import com.ewanghuiju.app.ui.my.activity.VipUpdataActivity;
import com.ewanghuiju.app.ui.my.adapter.VipCenterCantuanListAdapter;
import com.ewanghuiju.app.ui.my.adapter.VipCenterEquityListAdapter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.ArcViewTop;
import com.ewanghuiju.app.widget.popupwindow.PopupItemClickCallback;
import com.ewanghuiju.app.widget.popupwindow.VipTransformPopup;
import com.makeramen.roundedimageview.RoundedImageView;
import com.muddzdev.styleabletoast.StyleableToast;
import com.stx.xhb.androidx.OnDoubleClickListener;
import com.stx.xhb.androidx.XBanner;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 会员权益页面
 * @time 2020/2/16
 */
public class ShopmailFragment extends BaseFragment<ShopmailPresenter> implements ShopmailContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.mtoolbar)
    Toolbar mtoolbar;

    @BindView(R.id.xbanner)
    XBanner xbanner;
    @BindView(R.id.arcView)
    ArcViewTop arcView;
    @BindView(R.id.layout_top)
    RelativeLayout layoutTop;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.cradview)
    CardView cradview;
    @BindView(R.id.tv_ljyq)
    TextView tvLjyq;
    //权益列表
    @BindView(R.id.veRecyclerView)
    RecyclerView veRecyclerView;

    private VipCenterCantuanListAdapter vipCenterCantuanListAdapter;
    private VipCenterEquityListAdapter vipCenterEquityListAdapter;

    private VipTransformPopup vipTransformPopup;

    private VipEquityBean vipEquityBean;
    private int currentPosition = 0;

    public static ShopmailFragment newInstance() {

        Bundle bundle = new Bundle();

        ShopmailFragment fragment = new ShopmailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shopmail;
    }

    @Override
    protected void initEventAndData() {
        LoadingDialogUtils.show(activity);
        mPresenter.getData();
    }

    /**
     * 加载banner
     */
    public void initBanner(UserInfoResponBean userInfoResponBean) {
        //测试数据  测试bean
        List<PintuanHomeBannerBean> bannerList = new ArrayList<>();
        PintuanHomeBannerBean bean = new PintuanHomeBannerBean();
        bean.setResId(R.mipmap.vip_updata_common_bg);
        bean.setResMark(R.mipmap.vip_common_mark);
        bean.setTipTextColor(R.color.color_8b8c99);
        bean.setButtonBgRes(R.drawable.shape_trans_common_bg);
        bannerList.add(bean);

        bean = new PintuanHomeBannerBean();
        bean.setResId(R.mipmap.vip_updata_tzqy_bg);
        bean.setResMark(R.mipmap.icon_tuanzhang_mark);
        bean.setTipTextColor(R.color.color_b1934a);
        bean.setButtonBgRes(R.drawable.shape_trans_tz_bg);
        bannerList.add(bean);

        bean = new PintuanHomeBannerBean();
        bean.setResId(R.mipmap.vip_updata_qz_bg);
        bean.setResMark(R.mipmap.vip_qz_mark);
        bean.setTipTextColor(R.color.color_b68965);
        bean.setButtonBgRes(R.drawable.shape_trans_qz_bg);
        bannerList.add(bean);

        bean = new PintuanHomeBannerBean();
        bean.setResId(R.mipmap.vip_updata_daili_bg);
        bean.setResMark(R.mipmap.icon_area_daili_mark);
        bean.setTipTextColor(R.color.color_7754e7);
        bean.setButtonBgRes(R.drawable.shape_trans_daili_bg);
        bannerList.add(bean);

        xbanner.setBannerData(R.layout.banner_vip_center, bannerList);
        //加载广告图片
        xbanner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int mPosition) {
                //背景
                LinearLayout layoutMain = view.findViewById(R.id.layout_main);
                //转化按钮
                TextView tvVipTranform = view.findViewById(R.id.tv_vip_tranform);
                //当前等级
                TextView tvCorrent = view.findViewById(R.id.tv_corrent);
                //开团额度
                TextView tvKted = view.findViewById(R.id.tv_kted);
                //标志
                ImageView ivVipMark = view.findViewById(R.id.iv_vip_mark);
                //头像
                RoundedImageView ivHead = view.findViewById(R.id.iv_head);
                //购买代理
                TextView tvBuyDaili = view.findViewById(R.id.tv_buy_daili);

                tvKted.setTextColor(ContextCompat.getColor(context, bannerList.get(mPosition).getTipTextColor()));
                layoutMain.setBackgroundResource(bannerList.get(mPosition).getResId());
                tvVipTranform.setBackgroundResource(bannerList.get(mPosition).getButtonBgRes());
                ImageLoader.load(context, userInfoResponBean.getHeadimg(), ivHead);
                ivVipMark.setImageResource(bannerList.get(mPosition).getResMark());

                tvKted.setText("开团额度 " + (TextUtils.isEmpty(userInfoResponBean.getGroup_integral()) ? "0" : userInfoResponBean.getGroup_integral()) + "元");

                tvCorrent.setVisibility(userInfoResponBean.getIs_daili() == mPosition ? View.VISIBLE : View.INVISIBLE);
                tvKted.setVisibility(userInfoResponBean.getIs_daili() == mPosition ? View.VISIBLE : View.INVISIBLE);
                tvVipTranform.setVisibility(userInfoResponBean.getIs_daili() == mPosition ? View.VISIBLE : View.INVISIBLE);
                if (mPosition == 3) {
                    tvBuyDaili.setVisibility(View.VISIBLE);
                } else {
                    tvBuyDaili.setVisibility(View.GONE);
                }
                tvBuyDaili.setOnClickListener(new OnDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        //跳转到购买代理
                        new StartActivityUtil(context, VipUpdataActivity.class).startActivity(true);
                    }
                });
                tvVipTranform.setOnClickListener(new OnDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {


                        vipTransformPopup = new VipTransformPopup(context);
                        vipTransformPopup.setPopupGravity(Gravity.BOTTOM);
                        vipTransformPopup.setPopupItemClickCallback(
                                new PopupItemClickCallback() {
                                    @Override
                                    public void onSureCallback(String text) {
                                        if (TextUtils.isEmpty(text)) {
                                            StyleableToast.makeText(context, "请输入转化水滴数量", R.style.mytoast).show();
                                            return;
                                        }
                                        LoadingDialogUtils.show(activity);
                                        mPresenter.tranVipEquity(text);
                                    }

                                    @Override
                                    public void onTextChangedCallback(String text) {
                                        //文本监听
                                        if (vipTransformPopup != null) {
                                            vipTransformPopup.setTvKted("");
                                        }
                                        mPresenter.getPtigroupIntegralSum(text);
                                    }
                                }
                        );
                        vipTransformPopup.showPopupWindow();
//                        switch (mPosition) {
//                            case 0:
//                            case 1:
//                            case 2:
//                                break;
//                            case 3:
//                                break;
//                            default:
//                                break;
//                        }
                    }
                });
//                ImageView riv = view.findViewById(R.id.banner_image);
//                Glide.with(mContext).load(R.mipmap.test_banner).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(riv);
            }
        });
        xbanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;

                if (vipEquityBean == null) {
                    vipEquityBean = new VipEquityBean();
                }
                //加载数据
                changeVipEquityInfoAndRecyclerView(vipEquityBean);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        if (userInfoResponBean.getIs_daili() < bannerList.size()) {
            xbanner.setBannerCurrentItem(userInfoResponBean.getIs_daili());
        }
    }

    /**
     * 会员权益信息
     */
    private void initVipEquityInfo(List<VipEquityBean> vipEquityBeans) {
        if (vipEquityBeans.size() > 4) {
            vipEquityBeans = vipEquityBeans.subList(0, 4);
        }
        if (vipCenterEquityListAdapter != null) {
            vipCenterEquityListAdapter.setNewData(vipEquityBeans);
            return;
        }
        veRecyclerView.setLayoutManager(new GridLayoutManager(context, 4));
        vipCenterEquityListAdapter = new VipCenterEquityListAdapter(R.layout.item_vip_center_equity);
        veRecyclerView.setAdapter(vipCenterEquityListAdapter);
        vipCenterEquityListAdapter.setNewData(vipEquityBeans);
    }

    private void initRecyclerView(List<VipEquityBean> vipEquityBeans) {
        if (vipEquityBeans.size() > 0) {
            cradview.setVisibility(View.VISIBLE);
        } else {
            cradview.setVisibility(View.INVISIBLE);
        }
        if (vipCenterCantuanListAdapter != null) {
            vipCenterCantuanListAdapter.setNewData(vipEquityBeans);
            return;
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        vipCenterCantuanListAdapter = new VipCenterCantuanListAdapter(R.layout.item_vip_center_cantuan_list);
        recyclerView.setAdapter(vipCenterCantuanListAdapter);
        vipCenterCantuanListAdapter.setNewData(vipEquityBeans);

    }

    @OnClick({R.id.tv_ljyq, R.id.iv_right_button})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_ljyq:
                //立即邀请
//                new StartActivityUtil(context, InviteFriendActivity.class).startActivity(true);
                new StartActivityUtil(context, VipUpdataActivity.class).startActivity(true);
                break;
            case R.id.iv_right_button:
                //升级区域代理记录
//                new StartActivityUtil(context, VipUpdataOrderRecordActivity.class).startActivity(true);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void showContent(String list) {

    }

    @Override
    public void showMoreContent(String list) {

    }

    @Override
    public void showVipEquityList(VipEquityBean bean) {
        LoadingDialogUtils.dismissDialog_ios();
        vipEquityBean = bean;
        if (vipEquityBean == null) {
            vipEquityBean = new VipEquityBean();
        }
        //加载数据
        changeVipEquityInfoAndRecyclerView(vipEquityBean);
    }

    /**
     * 切换布局
     *
     * @param vipEquityBean
     */
    public void changeVipEquityInfoAndRecyclerView(VipEquityBean vipEquityBean) {
        List<VipEquityBean> vipEquityBeanList = new ArrayList<>();
        List<VipEquityBean> vipEquityCantuanList = new ArrayList<>();

        switch (currentPosition) {
            case 0:
                if (vipEquityBean.getJibie0() != null) {
                    vipEquityBeanList = vipEquityBean.getJibie0().getH();
                    vipEquityCantuanList = vipEquityBean.getJibie0().getS();
                }
                break;
            case 1:
                if (vipEquityBean.getJibie1() != null) {
                    vipEquityBeanList = vipEquityBean.getJibie1().getH();
                    vipEquityCantuanList = vipEquityBean.getJibie1().getS();
                }
                break;
            case 2:
                if (vipEquityBean.getJibie2() != null) {
                    vipEquityBeanList = vipEquityBean.getJibie2().getH();
                    vipEquityCantuanList = vipEquityBean.getJibie2().getS();
                }
                break;
            case 3:
                if (vipEquityBean.getJibie3() != null) {
                    vipEquityBeanList = vipEquityBean.getJibie3().getH();
                    vipEquityCantuanList = vipEquityBean.getJibie3().getS();
                }
                break;
            default:
                break;
        }

        if (vipEquityBeanList == null) {
            vipEquityBeanList = new ArrayList<>();
        }
        if (vipEquityCantuanList == null) {
            vipEquityCantuanList = new ArrayList<>();
        }
        initVipEquityInfo(vipEquityBeanList);
        initRecyclerView(vipEquityCantuanList);
    }

    @Override
    public void showIntegralSum(BaseResponse bean) {
        //转化成功
        if (vipTransformPopup != null) {
            vipTransformPopup.setTvKted("￥" + bean.getSum());
        }
    }

    @Override
    public void showTranVipEquity(BaseResponse bean) {
        //确认转化成功
        LoadingDialogUtils.dismissDialog_ios();
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_USERINFO));
        StyleableToast.makeText(context, "转化成功", R.style.mytoast).show();
        if (vipTransformPopup != null) {
            vipTransformPopup.dismiss();
        }
    }


    @Override
    public void showUserInfo(UserInfoResponBean bean) {
        //获取用户信息
        initBanner(bean);
        mPresenter.getVipEquity();
    }

    @Override
    public void showUserInfoError() {
        //获取用户信息失败

    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
    }
}

